/**
 * Copyright (c) 2007-2008 Nearchos Paspallis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * AspectSense
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, initiated by Nearchos Paspallis as
 * part of his PhD work at the University of Cyprus (http://www.ucy.ac.cy).
 *
 * More information about the project is available at "http://aspectsense.org".
 */

package org.aspectsense.framework;

import org.aspectsense.metadata.ServiceMetadata;
import org.aspectsense.metadata.ComponentMetadata;
import org.aspectsense.metadata.MetadataFactory;

import java.util.Set;
import java.util.HashSet;

/**
 * This is a data-structure allowing the formation of a collection of
 * {@link Component}s offering a particular {@link Service}.
 *
 * <p/>
 * Date: Sep 11, 2007<br/>
 * Time: 12:44:40 PM
 * </p>
 *
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 */
final class ServiceProviders
{
    private final Class serviceType;

    private Set<String> componentProviderIDs = new HashSet<String>();

    ServiceProviders(final Class serviceType)
    {
        if(serviceType == null)
        {
            throw new NullPointerException("Invalid null argumnet");
        }

        this.serviceType = serviceType;
    }

    /**
     * Returns a class corresponding to the type of the {@link Service} which
     * this instance refers to.
     *
     * @return an instance of {@link Class} corresponding to the
     * {@link Service} which this instance refers to.
     */
    public Class getServiceType()
    {
        return serviceType;
    }

    /**
     * Returns an array containing the IDs of the available components which
     * offer the {@link Service} of interest.
     *
     * @return an array containing the IDs of the available components offering
     * the {@link Service} of interest.
     */
    public String [] getServiceProviderIDs()
    {
        return componentProviderIDs.toArray(new String [componentProviderIDs.size()]);
    }

    public ServiceMetadata getServiceMetadata(final String componentID)
    {
        if(componentID == null)
        {
            throw new NullPointerException("Illegal null argument!");
        }
        if(!componentProviderIDs.contains(componentID))
        {
            throw new RuntimeException("Invalid componentID: " + componentID
                    + ", not registered as a provider of " + serviceType);
        }

        final Class componentType
                = Repository.getInstance().getComponentType(componentID);

        final ComponentMetadata componentMetadata
                = MetadataFactory.getComponentMetadata(componentType);

        return componentMetadata.getProvidedService();
    }

    boolean addServiceProvider(final String componentID)
    {
        return componentProviderIDs.add(componentID);
    }

    boolean removeServiceProvider(final String componentID)
    {
        return componentProviderIDs.remove(componentID);
    }

    public boolean isEmpty()
    {
        return componentProviderIDs.isEmpty();
    }

    public String toString()
    {
        return "Service: " + serviceType + " is provided by: " + componentProviderIDs;
    }
}